<?php
namespace logic\sys;
class Upload{
    
    /**
     * 处理base64编码的图片上传
     */
    public function base64($type='editor',$file){
        $img = base64_decode($file);
        $savePath = $this->getSavePath($type);
        
    }
    
    /**
     * 上传图片 本地服务器 => 图片服务器
     * 注意：上传图片时需把文件域字段【即$_FILES['file']】设置为file，否则上传不成功
     */
    public function images($type='default',$width=0,$height=0,$imgx=0,$imgy=0,$rotate=0){
        
        $result = $this->upFileLocal($type);    
        if( $width > 0  && $height > 0 && $result['ret']){
            $this->cropImg($result['filedir'],$width,$height,$imgx,$imgy,$rotate);
        }   
        
        if( $result['ret'] ){
            $imageSave = [
                'picurl'    => $result['filehttp'],
                'picname'   => $result['filename'],
                'size'      => $result['size'],
            ];
            $Image = new \logic\sys\images\Image();
            $Image->add($imageSave,$type);
        }
        return $result; 
        // TODO
        $localImgUrl = $this->upFileLocal($type);        
        $localImgUrl = $this->getLocalPath($localImgUrl);
        $url = $this->localToFtp($localImgUrl,$type);
        unlink($localImgUrl);
        return $url;
    }
    
    /**
     * 图片剪切
     * @param  integer $width      裁剪区域宽度
     * @param  integer $height     裁剪区域高度
     * @param  integer $x      裁剪区域x坐标
     * @param  integer $y      裁剪区域y坐标
     */
    private function cropImg($localImg,$width=0,$height=0,$imgx=0,$imgy=0,$rotate=0){
        $image = \think\Image::open($localImg);
        if( $rotate != 0 ) $image->rotate($rotate);
        $image->crop($width,$height,$imgx,$imgy)->save($localImg);
    }
    
    /**
     * 上传文件到本地服务器
    */
    public function upFileLocal($type='default',$fileName='file'){
        $fileObject = request()->file($fileName);
        if(!$fileObject) logicE('请选择上传文件');
        $savePath = $this->getSavePath($type);
        $Rusult = $fileObject->rule('uniqid')->move($savePath);
        if($Rusult){
            $file = rtrim($Rusult->getPath(),DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
            $file = $file.$Rusult->getSaveName();            
            $delimiter = \think\facade\Env::get('ROOT_PATH').'public';
            $files = explode($delimiter,$file);
            $result = [
                'ret'      => true,
                'filehttp' => Config('url.url.img').$files[1],
                'filedir'  => '.'.$files[1],
                'filename' => $Rusult->getInfo('name'),
                'size'     => $Rusult->getSize(),
            ];
        }else{
            // 上传失败获取错误信息
            $result = [
                'ret'   => false,
                'data'  => $fileObject->getError()
            ];
        }
        return $result;
    }
    
    /**
     * 获取保存文件的目录
     */
    private function getSavePath($type){
        if( !$type ) $type = 'default';
        $type = trim($type,DIRECTORY_SEPARATOR) .DIRECTORY_SEPARATOR;
        $saveFath = \think\facade\Env::get('ROOT_PATH').'public'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR;
        //$main = 'home'.DIRECTORY_SEPARATOR;
        $dataPath = date('Y').DIRECTORY_SEPARATOR.date('m').DIRECTORY_SEPARATOR.date('d').DIRECTORY_SEPARATOR;
        
        $Passport = \logic\user\Passport::getInstance();
        $plat = $Passport->plat;
        // 2 代理平台 3 商户平台 4 用户展示平台
        $platMap = [ 1=> 'r', 2=>'a' , 3=> 'm' ,4 => 'u' ];
        $User = $Passport->checkLogin();
        
        if( $User ){
            if( $User->role == 'site' ){
                $userId = $User->getInfo()['msiteid'];
            }else{
                $userId = $User->userId;
            }            
            $platPath = $platMap[$plat].$userId.DIRECTORY_SEPARATOR;
        }else{
            $platPath = $platMap[$plat].DIRECTORY_SEPARATOR;
        }
        $saveFath = $saveFath.$type.$dataPath.$platPath;
        return $saveFath;
    }
    
}